<template>
  <div class="container">
    <el-form ref="postForm" :model="postForm" :rules="rules" label-position="top" label-width="100px" class="tweets-form">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="选择素材" prop="mediaid">
            <el-select
              v-model="postForm.mediaid"
              filterable
              placeholder="请选择"
              style="width:100%"
              @change="fetchMaterials"
            >
              <el-option
                v-for="item in mediaIdOptions"
                :key="item.id"
                :label="item.title"
                :value="item.wzmediaid"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-card v-if="mediaList.length > 0" class="box-card">
      <div slot="header" class="card-header clearfix">
        <h5 class="title pull-left">素材预览</h5>
        <!-- <div class="pull-right">
          <span class="tips text-warning">拖动单个素材，可进行排序</span>
        </div> -->
      </div>
      <div class="card-wrapper text-center">
        <div class="media-card">
          <draggable v-model="mediaList">
            <transition-group>
              <div
                v-for="(item, index) in mediaList"
                :key="item.id"
                class="media-item"
                :class="index === 0 ? 'first' : ''"
              >
                <div class="item-title">
                  <router-link :to="`/materials/list/${item.id}`">{{ item.title }}</router-link>
                </div>
                <div class="item-cover">
                  <img :src="item.cover" :alt="item.title" class="cover-img">
                  <div v-if="index === 0" class="cover-mask" />
                </div>
              </div>
            </transition-group>
          </draggable>
        </div>
      </div>
    </el-card>

    <div class="text-center mt40">
      <el-button @click="prevStep">上一步</el-button>
      <el-button
        type="primary"
        @click="submitTask"
      >提交任务</el-button>
    </div>
  </div>
</template>

<script>
import {
  getTweetsList,
  submitTask
} from '@/api/tweets'
import {
  getAllMaterials
} from '@/api/baseData'
import {
  getMaterials
} from '@/api/materials'
import draggable from 'vuedraggable'

const defaultForm = {
  id: undefined,
  mediaid: ''
}

export default {
  name: 'TweetTaskStep3',
  components: {
    draggable
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      taskId: '',
      mediaIdOptions: [],
      mediaList: [],
      postForm: Object.assign({}, defaultForm),
      loading: false,
      rules: {
        mediaid: [
          { required: true, message: '请选择素材', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      this.taskId = id
      this.fetchData(id)
    }
    this.fetchAllMaterials()
  },
  methods: {
    fetchAllMaterials() {
      getAllMaterials({
        type: 4
      }).then(response => {
        // console.log(response)
        this.mediaIdOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchMaterials() {
      getMaterials({
        wzmediaid: this.postForm.mediaid
      }).then(response => {
        // console.log(response)
        this.mediaList = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchData(id) {
      getTweetsList({ id }).then(response => {
        const { data } = response
        // console.log(data)
        this.postForm.id = data.list[0].id
        this.postForm.mediaid = data.list[0].mediaid
        if (!this._.isEmpty(data.list[0].mediaid)) {
          this.fetchMaterials()
        }
      }).catch(err => {
        console.log(err)
      })
    },
    prevStep() {
      this.$router.push(`/tweets/edit/${this.taskId}?step=2`)
    },
    submitTask() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          submitTask(this.postForm).then(response => {
            // console.log(response)
            this.$notify({
              title: '成功',
              message: '提交任务成功',
              type: 'success',
              duration: 2000
            })
            this.loading = false
            this.$router.push('./../list')
          }).catch(err => {
            console.log(err)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 40px 140px;
}
.tweets-form {
  padding: 40px 0;
}
</style>
